#ifndef _BYTEORDER_H_
#define _BYTEORDER_H_

#include "medved/util.hpp"
#include "medved/typelist.hpp"
#include "medved/type_traits.hpp"

namespace Private
{
	template <int sizeOf, bool toNetOrder, class T>
	void convert(T &val);
	
	template <>
	void convert<4, true, int>(int &val);
	
	template <>
	void convert<4, false, int>(int &val);
	
	template <>
	void convert<2, true, short>(short &val);
	
	template <>
	void convert<2, false, short>(short &val);
	
	template <>
	void convert<1, true, char>(char &val);
	
	template <>
	void convert<1, false, char>(char &val);
		
	typedef Medved::Seq2TypeList<Medved::Seq<
	Medved::NullType, char, short, Medved::NullType, int
	> >::Result Size2Type;

	template <bool dir>
	struct ChangeByteOrder
	{
		template <class T>
		const ChangeByteOrder &operator ()(T &data) const
		{
			//static assert
			char dummy[Medved::TypeTraits<T>::isInt]; (void)dummy;

			convert<sizeof(T), dir>(reinterpret_cast
			<typename Medved::TypeAt<Size2Type, sizeof(T)>::Result &>(data));
			return *this;
		}
	};
}

typedef Private::ChangeByteOrder<true> ToNetByteOrder;
typedef Private::ChangeByteOrder<false> ToHostByteOrder;

#endif
